<template>
    <a-card hoverable>
        <a-form ref="formRef" :model="form" :rules="rules" :labelCol="{ span: 6 }">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="编码" name="id">
                        <a-input style="width: 100%" v-model:value="form.id" disabled />
                    </a-form-item>
                    <a-form-item label="SMTP地址" name="smtpHost">
                        <a-input style="width: 100%" placeholder="请输入SMTP地址" v-model:value="form.smtpHost" />
                    </a-form-item>
                    <a-form-item label="账号" name="account">
                        <a-input style="width: 100%" placeholder="请输入账号" v-model:value="form.account" />
                    </a-form-item>
                    <a-form-item label="协议" name="protocol">
                        <a-input style="width: 100%" placeholder="请输入基本DN" v-model:value="form.protocol" />
                    </a-form-item>

                    <a-form-item label="发送人" name="sender">
                        <a-input style="width: 100%" placeholder="请输入AD域名" v-model:value="form.sender" />
                    </a-form-item>

                </a-col>
                <a-col :span="12">
                    <a-form-item label="状态" name="status">
                        <a-switch v-model:checked="form.status">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                    </a-form-item>

                    <a-form-item label="端口" name="port">
                        <a-input-number style="width: 100%" placeholder="请输入凭证" v-model:value="form.port" />
                    </a-form-item>
                    <a-form-item label="凭证" name="credentials">
                        <a-input-password style="width: 100%"  v-model:value="form.credentials" />
                    </a-form-item>
                    <a-form-item label="编码" name="encoding">
                        <a-input style="width: 100%" placeholder="请输入AD域名" v-model:value="form.encoding" />
                    </a-form-item>
                    <a-form-item label="SSL" name="sslSwitch" v-if="form.sslSwitch">
                        <a-switch v-model:checked="form.sslSwitch">
                            <template #checkedChildren><check-outlined /></template>
                            <template #unCheckedChildren><close-outlined /></template>
                        </a-switch>
                       
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
        <div style="text-align: center;">
            <a-button type="primary" @click="submit">提交</a-button>

        </div>
    </a-card>
</template>

<script setup>
import { onMounted, reactive, ref, watch, createVNode } from 'vue';
import { emailsendersApi } from '/src/api/security/emailsenders/api';
import { Modal, message } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import _ from 'lodash';
import { SmartLoading } from '/src/components/framework/smart-loading';
import { smartSentry } from '/src/lib/smart-sentry';


// ----------------------- 表单 ---------------------

const formRef = ref();


const formDefault = {
    id: undefined,
    smtpHost: undefined,
    account: undefined,
    protocol: undefined,
    sender: undefined,
    port: undefined,
    status: 0,
    encoding: undefined,
    credentials: undefined,
    filters: undefined,
    sslSwitch: undefined,
    trustStore: undefined,
    trustStorePassword: undefined
};

let form = reactive({ ...formDefault });

// 表单规则
const rules = {
    id: [{ required: true, message: '请选择产品' }],
    smtpHost: [{ required: true, message: '请选择产品' }],
    account: [{ required: true, message: '请输入地址' }],
    protocol: [{ required: true, message: '请输入账号' }],
    sender: [{ required: true, message: '请输入基本DN' }],
    port: [{ required: true, message: '请输入凭证' }],
    credentials: [{ required: true, message: '请选择域账号' }],
    status: [{ required: true, message: '请选择状态' }],
    encoding: [{ required: true, message: '请输入状态' }],
    sslSwitch: [{ required: true, message: '请输入AD域名' }],
};



//

// ----------------------- 部门树的展示 ---------------------
async function submit() {
 
    form.status = form.status ? 1 : 0
    form.sslSwitch = form.sslSwitch ? 1 : 0
    let res = await emailsendersApi.updateemailsenders(form);
    if (res.code == 0) {
        message.success("修改成功")
        init();
    }
}

onMounted(() => {
    init();
});

// 
async function init() {
    try {

        let res = await emailsendersApi.getemailsenders();
        if (res.code == 0) {
            form.id = res.data.id;
            form.smtpHost = res.data.smtpHost;
            form.account = res.data.account
            form.protocol = res.data.protocol
            form.sender = res.data.sender
            form.port = res.data.port
            form.encoding = res.data.encoding
            form.status = res.data.status
            form.status = form.status == 1 ? true : false
            form.sslSwitch = res.data.sslSwitch
         
            form.sslSwitch = form.sslSwitch == 1 ? true : false
        }
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        tableLoading.value = false;
    }
}



// 重置
function resetQuery() {
    queryParam.value.providerName = ""
    queryParam.value.pageSize = 10
    queryParam.value.pageNumber = 1
    queryParam.value.total = 0
    onSearch();
}

// 搜索
function onSearch() {
    init();
}



// ----------------------- 表单操作：添加部门/修改部门/删除部门/上下移动 ---------------------


// 编辑
function updateRecord(e) {
    getRecordById(e);
    title.value = "编辑"
    visible.value = true
}

async function getRecordById(id) {
    let res = await socialsproviderApi.getSocialsProviderById(id);
    disabledCode.value = true
    if (res.code == 0) {
        form.id = res.data.id
        form.icon = res.data.icon
        form.provider = res.data.provider
        form.providerName = res.data.providerName
        form.clientId = res.data.clientId
        form.clientSecret = res.data.clientSecret
        form.agentId = res.data.agentId
        form.scanCode = res.data.scanCode
        form.display = res.data.display
        form.sortIndex = res.data.sortIndex
        form.status = res.data.status

    }

}

// 删除
function deleterRecord(id) {
    Modal.confirm({
        title: '提醒',
        icon: createVNode(ExclamationCircleOutlined),
        content: '确定要删除吗?',
        okText: '删除',
        okType: 'danger',
        async onOk() {
            SmartLoading.show();
            try {
                await socialsproviderApi.deleteSocialsProvider(id);
                init();
            } catch (error) {
                smartSentry.captureError(error);
            } finally {
                SmartLoading.hide();
            }
        },
        cancelText: '取消',
        onCancel() { },
    });
}
</script>

<style scoped lang="less"></style>